package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"group-card/common/constant"
	"group-card/common/request"
	"group-card/model/entity"
	"group-card/rpc/card/cardclient"
	"group-card/rpc/card/internal/svc"
)

type TraceabilityListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewTraceabilityListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TraceabilityListLogic {
	return &TraceabilityListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *TraceabilityListLogic) TraceabilityList(in *cardclient.TraceabilityListRequest) (*cardclient.TraceabilityListResponse, error) {
	var ctx context.Context
	req := &entity.CardTraceabilityReq{
		BaseReq: request.BaseReq{
			PageNum:  in.PageNum,
			PageSize: in.PageSize,
		},
	}
	data, count, err := l.svcCtx.TraceabilityModel.List(ctx, req)
	if err != nil {
		return nil, err
	}

	var list []*cardclient.TraceabilityListData
	for _, value := range data {
		list = append(list, &cardclient.TraceabilityListData{
			Id:             value.Id,
			CardNumber:     value.CardNumber,
			StorageAdminId: value.StorageAdminId,
			StorageTime:    value.StorageTime.Format(constant.YMDHIS),
			PublishAdminId: value.PublishAdminId,
			PublishTime:    value.PublishTime.Format(constant.YMDHIS),
			ReceiveAdminId: value.ReceiveAdminId,
			ReceiveTime:    value.ReceiveTime.Format(constant.YMDHIS),
			BindingUserId:  value.BindingUserId,
			BindingTime:    value.BindingTime.Format(constant.YMDHIS),
			WriteOffTime:   value.WriteOffTime.Format(constant.YMDHIS),
			Status:         value.Status,
			CreateAt:       value.CreateAt.Format(constant.YMDHIS),
			UpdateAt:       value.UpdateAt.Format(constant.YMDHIS),
			DeleteAt:       value.DeleteAt.Format(constant.YMDHIS),
		})
	}

	return &cardclient.TraceabilityListResponse{
		Total: count,
		List:  list,
	}, nil

}
